package com.ilop.sthome.ui.fragment.message;

import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import android.view.View;

import com.example.common.base.BaseBindFragment;
import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.example.common.base.MyPagerAdapter;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.FragmentMessageBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * @author skygge
 * @date 2020-04-01.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：消息页面
 */
public class MessageFragment extends BaseBindFragment<FragmentMessageBinding> {

    private List<Fragment> mFragment;
    private String[] mTitle;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_message;
    }

    @Override
    protected void initView() {
        super.initView();
        mFragment = new ArrayList<>();
        mTitle = new String[]{getString(R.string.warn_logs), getString(R.string.device_share)};
        mDBind.mainDeleteShare.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {
        super.initData();
        mFragment.add(new AlarmLogFragment());
        mFragment.add(new DeviceShareFragment());
        MyPagerAdapter mAdapter = new MyPagerAdapter(getChildFragmentManager());
        mAdapter.setTitleAndFragment(mFragment);
        mDBind.messageViewPager.setAdapter(mAdapter);
        mDBind.messageTabLayout.addTab(mTitle[0]);
        mDBind.messageTabLayout.addTab(mTitle[1]);
        mDBind.messageTabLayout.setupWithViewPager(mDBind.messageViewPager);
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with(CommonId.KEY_MESSAGE_TYPE, Integer.class).observe(this, integer -> {
            assert integer!=null;
            if (integer == 1){
                if (mDBind.messageViewPager.getCurrentItem() == 0){
                    LiveDataBus.get().with(CommonId.REFRESH_ALARM).setValue(0);
                }else {
                    mDBind.messageViewPager.setCurrentItem(0);
                }
            }else if (integer == 2){
                mDBind.messageViewPager.setCurrentItem(1);
                LiveDataBus.get().with(CommonId.SHARE_REFRESH).setValue(0);
            }
        });
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.messageTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getPosition() == 0){
                    mDBind.mainDeleteShare.setVisibility(View.GONE);
                }else {
                    mDBind.mainDeleteShare.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mDBind.messageViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mDBind.messageTabLayout.getTabLayout()));

        mDBind.mainDeleteShare.setOnClickListener(v -> LiveDataBus.get().with(CommonId.SHARE_CLEAR).setValue(0));
    }
}
